import crtkc from "./crtkc";
import { User } from "../scripts/UserManager";
import { UserData } from "../scripts/UserConst";
import { Tip } from "../scripts/TipManager";
import { Event } from "../scripts/EventManager";
import { default as GameEvent } from "../scripts/EventConst";
const {ccclass, property} = cc._decorator;

@ccclass
export default class sliderBarView extends cc.Component {

    @property(cc.Label)
    lab_slider: cc.Label = null;

    @property(cc.Node)
    node_btn: cc.Node = null;

    @property(cc.Label)
    lab_game:cc.Label = null;



    hasGet: boolean = false;
    start () {
        let hasGet = crtkc.getData('sidebar', { today: '', canGet: '0' });
        let date = new Date();
        let today = date.getUTCFullYear() + "-" + date.getUTCMonth() + "-" + date.getUTCDay();
        if (hasGet.today == today) {
            this.hasGet = true;
            this.lab_slider.string = "已领取";
        }else if(hasGet.canGet == '1'){
            this.hasGet = false;
            this.lab_slider.string = "领取"
        } 
        else {
            this.hasGet = false;
            this.lab_slider.string = "从侧边栏打开";
            this.node_btn.active = false;
        }

        //@ts-ignore
        let channel = window.__prv.getChannel();
        console.error("channel: ", channel);
        switch(channel){
            case 'ks':
                this.lab_game.string = '如花的奇葩事';
                break;
            case 'ks1':
                this.lab_game.string = '快救救孩子吧';
                break;
            case 'ks2':
                this.lab_game.string = '疯狂大作战';
                break;
            case 'ks3':
                this.lab_game.string = '全民求生';
                break;
        }

    }

    private close(){
        this.node.destroy();
    }

    private clickSidebar(){
        let hasGetOv = crtkc.getData('sidebar', { today: '', canGet: '0' });
        let date = new Date();
        let today = date.getUTCFullYear() + "-" + date.getUTCMonth() + "-" + date.getUTCDay();
        if (this.hasGet) {
            console.log("今日奖励已领取");
        } else if (hasGetOv.canGet == '0') {
            console.error("从侧边栏打开");
        } else if (today == hasGetOv.today) {
            console.log("今日奖励已领取");
        } else {
            this.hasGet = true;
            hasGetOv.today = today;
            crtkc.saveData('sidebar', hasGetOv);
            this.lab_slider.string = "已领取";
            // // // 增加体力值的逻辑
            // let power = User.get(UserData.POWER);
            // let t = 1; // 这里的t值需要根据实际情况调整
            // User.set(UserData.POWER, power + 10 * t);
            // Tip.show("体力+" + 10 * t);
            // Event.emit(GameEvent.POWER_EFFECT, true);
            //增加钥匙逻辑
            let key = User.get(UserData.KEY);
            User.set(UserData.KEY, key + 1);
            Tip.show("钥匙+" + 1);
            Event.emit(GameEvent.KEY_EFFECT, true);
        }
    }

}
